package CreatorPattern.Factory;

/**
 * @author: Themberfue
 * @date: 2025/8/10 11:21
 * @description:
 */
// 1. 日志记录器接口（产品接口）
interface Logger {
    void log(String message);
}

// 2. 具体日志实现（具体产品）
class FileLogger implements Logger {
    @Override
    public void log(String message) {
        System.out.println("文件日志: " + message);
    }
}

class ConsoleLogger implements Logger {
    @Override
    public void log(String message) {
        System.out.println("控制台日志: " + message);
    }
}

class DatabaseLogger implements Logger {
    @Override
    public void log(String message) {
        System.out.println("数据库日志: " + message);
    }
}

// 3. 简单工厂模式
class SimpleLoggerFactory {
    // 根据类型创建不同的日志记录器
    public static Logger createLogger(String type) {
        switch (type.toLowerCase()) {
            case "file":
                return new FileLogger();
            case "console":
                return new ConsoleLogger();
            case "database":
                return new DatabaseLogger();
            default:
                throw new IllegalArgumentException("不支持的日志类型: " + type);
        }
    }
}

// 4. 工厂方法模式
interface LoggerFactory {
    Logger createLogger();
}

class FileLoggerFactory implements LoggerFactory {
    @Override
    public Logger createLogger() {
        return new FileLogger();
    }
}

class ConsoleLoggerFactory implements LoggerFactory {
    @Override
    public Logger createLogger() {
        return new ConsoleLogger();
    }
}

class DatabaseLoggerFactory implements LoggerFactory {
    @Override
    public Logger createLogger() {
        return new DatabaseLogger();
    }
}

// 5. 抽象工厂模式（扩展日志格式）
interface LoggerAbstractFactory {
    Logger createLogger();
    Formatter createFormatter();
}

interface Formatter {
    String format(String message);
}

class SimpleFormatter implements Formatter {
    @Override
    public String format(String message) {
        return message;
    }
}

class TimestampFormatter implements Formatter {
    @Override
    public String format(String message) {
        return "[" + System.currentTimeMillis() + "] " + message;
    }
}

class AdvancedFileLoggerFactory implements LoggerAbstractFactory {
    @Override
    public Logger createLogger() {
        return new FileLogger();
    }

    @Override
    public Formatter createFormatter() {
        return new TimestampFormatter();
    }
}

class BasicConsoleLoggerFactory implements LoggerAbstractFactory {
    @Override
    public Logger createLogger() {
        return new ConsoleLogger();
    }

    @Override
    public Formatter createFormatter() {
        return new SimpleFormatter();
    }
}

// 6. 客户端代码
public class FactoryExample {
    public static void main(String[] args) {
        String message = "系统启动成功";

        // 使用简单工厂模式
        System.out.println("=== 简单工厂模式 ===");
        Logger fileLogger = SimpleLoggerFactory.createLogger("file");
        fileLogger.log(message);

        // 使用工厂方法模式
        System.out.println("\n=== 工厂方法模式 ===");
        LoggerFactory consoleFactory = new ConsoleLoggerFactory();
        Logger consoleLogger = consoleFactory.createLogger();
        consoleLogger.log(message);

        // 使用抽象工厂模式
        System.out.println("\n=== 抽象工厂模式 ===");
        LoggerAbstractFactory advancedFactory = new AdvancedFileLoggerFactory();
        Logger advancedLogger = advancedFactory.createLogger();
        Formatter formatter = advancedFactory.createFormatter();
        advancedLogger.log(formatter.format(message));
    }
}

